
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32")
#pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#include <string>
using namespace std;
#pragma comment(lib, "winmm")
#include <strsafe.h>

#include "Window.h"
#include "Toolbar.h"
#include "Control.h"
#include "Gif.h"

#define tb_height 24

MCI_OPEN_PARMS op; MCI_PLAY_PARMS pp; MCI_GENERIC_PARMS gp; Obj appw, b, tb, c, g;

//
//void deb(char *f, ...)
//{ char str[100]; char* pc; _crt_va_start(pc, f); vsprintf_s(str, f, pc); _crt_va_end(pc); OutputDebugStringA(str); }


//
class appWindow: public Window
{
   NMHDR nmh;

public:

   int OnPaint(HDC);
   int OnCommand(WPARAM, LPARAM);
   //LONG_PTR OnSize(WPARAM, LPARAM);
   int OnMciNotify(WPARAM, LPARAM);
};


//start application
int __stdcall WinMain(HINSTANCE hi, HINSTANCE, LPSTR, int)
{
   HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
   CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
   //HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);

   //
   appw = new appWindow(); //main app window
   appw->Create(0, CS_HREDRAW|CS_VREDRAW, WS_EX_ACCEPTFILES, WS_OVERLAPPEDWINDOW|MM_MCINOTIFY, L"Window", 1, 111, 555, 444, 0, 0);
   appw->setIcon(1);
   appw->setText(L"OOP demo");
   appw->Show(1, 1);

   //
   g = new Gif();
   g->Create(appw, 0, 0, WS_VISIBLE|WS_CHILD|WS_BORDER, L"Window", 200, 200, 24, 24, appw->hwnd, 133);
   //((Control*)c)->Initialize();
   g->Show();

   //button
   b = new Control();
   b->Create(appw, 0, 0, WS_VISIBLE|WS_CHILD, L"button", 200, 50, 100, 33, appw->hwnd, 11);
   b->setText(L"Button");
   b->Show(1);

   //checkmark
   c = new Control();
   c->Create(appw, 0, 0, WS_VISIBLE|WS_CHILD, L"Window", 100, 150, 24, 24, appw->hwnd, 12);
   ((Control*)c)->Initialize();
   c->Show();


   //
   tb = new Toolbar();
   tb->Create(appw, 0, 0, WS_VISIBLE|WS_CHILD, L"Window", 0, 0, appw->getClientWidth(), tb_height, appw->hwnd, 13);
   ((Toolbar*)tb)->Initialize();
   tb->Show(1, 0);

   //
   MSG msg; //HACCEL appA = LoadAcceleratorsA(hi, MAKEINTRESOURCEA(1));
   ACCEL a[1]; a[0].fVirt = 0; a[0].cmd = 22; a[0].key = VK_ESCAPE; HACCEL appA = CreateAcceleratorTableW(a,1);

   //
   while(GetMessageW(&msg,0,0,0))
   { if(!TranslateAcceleratorW(msg.hwnd, appA, &msg)) { TranslateMessage(&msg); DispatchMessageW(&msg); } }

   CoUninitialize();

   return (int)msg.wParam;
}


//
//int appWindow::OnNotify(WPARAM wp, LPARAM lp)
//{ 
//   //if((((LPNMHDR)lp)->code == 7777)) tb->OnClose();
//   return 0;
//}


//
//int appWindow::OnUserMessage(UINT m, WPARAM, LPARAM)
//{
//   //if(m == WM_USER+5) msg(L"5");
//   return 0;
//}


//
int appWindow::OnCommand(WPARAM wp, LPARAM lp)
{
   switch(LOWORD(wp))
   {

   case 11:

      msg(L"ok");

      ////nmh.code = 7777; nmh.idFrom = 11; nmh.hwndFrom = b->hwnd;
      ////SendMsg(b->hpar, WM_NOTIFY, (WPARAM)b->hwnd, (LPARAM)&nmh);

      //if(c)
      //{
      //   c->Close();
      //   c = nullptr;
      //}

      break;

   case 22: msg(L"esc"); break;

   case 678: //comenzi butoane toolbar

      switch(LOWORD(lp))
      {

      case 0: //open

         mciSendStringW(L"open ShaniaTwain.mp3 type mpegvideo", 0, 0, 0);
         setText(L"ShaniaTwain.mp3");

         break;

      case 1: //play

         mciSendStringW(L"status ShaniaTwain.mp3 mode", buf, 111, 0);

         if( wcscmp(buf, L"playing") ) //daca e stop
         {
            pp.dwCallback = (DWORD)hwnd;
            mciSendCommandW(MCI_ALL_DEVICE_ID, MCI_PLAY, MCI_NOTIFY, (DWORD)&pp);
         }
         else mciSendStringW(L"resume ShaniaTwain.mp3", 0, 0, 0); //daca e pauza

         break;

      case 2: //pauza

         mciSendCommandW(MCI_ALL_DEVICE_ID, MCI_PAUSE, 0, 0); //pause ShaniaTwain.mp3

         break;

      case 3: //stop

         mciSendCommandW(MCI_ALL_DEVICE_ID, MCI_CLOSE, 0, 0); //close all
         setText(L"");

         break;

      }

      break;
   }

   return 0;
}

//
int __stdcall EnumChildProc(HWND c, LPARAM lp) 
{ 
   LPRECT pr = (LPRECT)lp; 
   if(GetDlgCtrlID(c) == 13) MoveWindow(c, 0, 0, pr->right, 24, 1); //13 e toolbarul
   return 1;
}


//
//LONG_PTR appWindow::OnSize(WPARAM wp, LPARAM lp)
//{
//   if(!IsIconic(hwnd))
//   {
//      RECT r; GetClientRect(hwnd, &r);
//      EnumChildWindows(hwnd, EnumChildProc, (LPARAM)&r); 
//   }
//   return 0L;
//}


//
int appWindow::OnMciNotify(WPARAM wp, LPARAM lp)
{
   if(wp == MCI_NOTIFY_SUCCESSFUL) //la terminare melodie
   {
      SendMessageW(tb->hwnd, WM_COMMAND, 345, MAKELONG(0,0));
      SendMessageW(tb->hwnd, WM_COMMAND, 345, MAKELONG(1,3));
      SendMessageW(tb->hwnd, WM_COMMAND, 345, MAKELONG(2,3));
      SendMessageW(tb->hwnd, WM_COMMAND, 345, MAKELONG(3,3));
      InvalidateHwnd(tb->hwnd);
      mciSendCommandW(MCI_ALL_DEVICE_ID, MCI_CLOSE, 0, 0);
      setText(L"");
   }
   return 0;
}


//
int appWindow::OnPaint(HDC hdc)
{
   SelectObject(hdc, CreatePen(PS_SOLID, 1, RGB(128,128,128))); //culoare

   MoveToEx(hdc, 0, tb_height, 0);
   LineTo(hdc, appw->getClientWidth(), tb_height);

   MoveToEx(hdc, 55, 55, 0);
   LineTo(hdc, 88, 88);

   Rectangle(hdc, 10, 333, 56, 55);

   return 0;
}
